const { request } = require("../../services/request");

Page({
    /**
     * 页面的初始数据
     */
    data: {
        loginBannerBg: "../../assets/images/login-bannder-bg.png",
        agree: false,
        phonenumber: "",
        captcha: "",
        captchaCountdown: 60,
        captchaText: "获取验证码",
        t: null, //计时器
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {},

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {},

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {},

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {},

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {},

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {},

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {},

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {},
    handelInput(e) {
        console.log("e :>> ", e);
        this.setData({
            phonenumber: e.detail.value,
        });
    },

    handelInputCaptcha(e) {
        this.setData({
            captcha: e.detail.value,
        });
    },
    handelGetCaptcha() {
        let { captchaCountdown } = this.data;
        console.log("captchaCountdown :>> ", captchaCountdown);
        var that = this;
        if (captchaCountdown === 60) {
            //获取验证码
            //倒计时
            let t = setInterval(() => {
                console.log("captchaCountdown :>> ", captchaCountdown);
                console.log("captchaCountdown :>> ", captchaCountdown);
                if (captchaCountdown == 0) {
                    clearInterval(t);
                    that.setData({
                        captchaCountdown: 60,
                        captchaText: "获取验证码",
                    });
                } else {
                    captchaCountdown -= 1;
                    that.setData({
                        captchaCountdown: captchaCountdown - 1,
                        captchaText: captchaCountdown + "s",
                    });
                }
            }, 1000);
        }
    },
    handleAgree() {
        this.data.agree = !this.data.agree;
    },
    handleToPolicy() {
        wx.navigateTo({
            url: "/pages/policy/index",
        });
    },
    handleLoginBtn() {
        let valid = this.validate();
        if (valid) {
            wx.login({
                success: (res) => {
                    request("post", "/authorization/wx/registerOrLogin", {
                            jsCode: res.code,
                            nickName: "测试",
                        })
                        .then((logginRes) => {
                            const { data } = logginRes;
                            const { token, userInfo } = data;
                            console.log(userInfo);
                            wx.setStorageSync("token", token);
                            try {
                                wx.setStorageSync("userInfo", JSON.stringify(userInfo));
                            } catch (error) {
                                console.log("error", error);
                            }
                            wx.switchTab({
                                url: "/pages/home/index",
                                success: (res) => {},
                                fail: (err) => {},
                            });
                        })
                        .catch((err) => {});
                },
            });
        }
    },
    validate() {
        const { phonenumber, captcha, agree } = this.data;
        if (phonenumber == "") {
            wx.showToast({
                title: "请检查手机号码",
            });
            return false;
        }
        if (captcha == "") {
            wx.showToast({
                title: "请检查验证码",
            });
            return false;
        }
        if (!agree) {
            wx.showToast({
                title: "请先同意协议",
            });
            return false;
        }
        return true;
    },
    handelgotoWxLogin() {
        console.log("goto :>> ");
        wx.navigateTo({
            url: "/pages/login/index",
            success: () => {},

            fail: (err) => {
                console.log("err :>> ", err);
            },
        });
    },
});